package com.yinp.proapp.fragment.bottompage;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yinp.proapp.R;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.AppBaseFragment;
import com.yinp.proapp.bean.ToolsTopBean;
import com.yinp.proapp.databinding.FragmentToolsBinding;
import com.yinp.proapp.databinding.ItemToolsTopListBinding;
import com.yinp.proapp.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

public class ToolsFragment extends AppBaseFragment<FragmentToolsBinding> {
    @Override
    protected void initViews(View view) {
        initRecycler();
    }

    private List<ToolsTopBean> topList = new ArrayList<>();
    private CommonAdapter<ToolsTopBean> topAdapter;

    private void initRecycler() {
        topList.add(new ToolsTopBean("百度地图", R.mipmap.map, "com.yinp.proapp.module.baidu_map.activity.BaiduMapActivity"));
        topList.add(new ToolsTopBean("计算器", R.mipmap.calculator, ""));
        topList.add(new ToolsTopBean("日历待办", R.mipmap.calendar, "com.yinp.proapp.module.calendar.activity.CalendarAddActivity"));
        topList.add(new ToolsTopBean("QQ", R.mipmap.qq, "com.yinp.proapp.module.qq.activity.QQLoginActivity"));
        topList.add(new ToolsTopBean("二维码扫描", R.mipmap.qrcode, "com.yinp.proapp.module.erweima.activity.MQRCodeActivity"));
//        topList.add(new ToolsTopBean("二维码扫描", R.mipmap.qrcode, "com.king.zxing.CaptureActivity"));
        topAdapter = new CommonAdapter<ToolsTopBean>(getContext(), topList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemToolsTopListBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, ToolsTopBean item) {
                ItemToolsTopListBinding binding = (ItemToolsTopListBinding) holder.getBinding();
                if (item.getDrawableId() == -1) {
                    binding.ivImg.setBackgroundResource(R.mipmap.default1);
                } else {
                    binding.ivImg.setBackgroundResource(item.getDrawableId());
                }
                binding.tvTitle.setText(item.getTitle());
            }
        };
        topAdapter.setOnItemClickListener((position, view) -> {
            if (!TextUtils.isEmpty(topList.get(position).getUrl())) {
                Intent intent = new Intent();
                intent.setClassName(getContext(), topList.get(position).getUrl());
                goToActivity(intent);
            }
        });
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        bd.rvList.setLayoutManager(gridLayoutManager);
        bd.rvList.addItemDecoration(new SpaceItemDecoration(3));
        bd.rvList.setAdapter(topAdapter);
    }

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        private int space;  //位移间距

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildAdapterPosition(view) % space == 0) {
                outRect.left = AppUtils.dpToPx(20);
                outRect.right = AppUtils.dpToPx(10);
            } else if (parent.getChildAdapterPosition(view) % space == 1) {
                outRect.left = AppUtils.dpToPx(10);
                outRect.right = AppUtils.dpToPx(10);
            } else {
                outRect.left = AppUtils.dpToPx(10);
                outRect.right = AppUtils.dpToPx(20);
            }
        }

    }
}
